package com.dongqi.myblog.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongqi.myblog.po.TType;
import com.dongqi.myblog.service.TTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dongqi
 * @since 2020-08-03
 */
@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    private TTypeService typeService;

    /**
     * 分页查询type（根据id降序）
     * @return
     */
    @GetMapping("/types")
    public String pageTypes(@RequestParam(defaultValue = "1") Long pageNo,
                            @RequestParam(defaultValue = "8") Long pageSize,
                            Model model){

        Page<TType> page = typeService.pageType(pageNo, pageSize);
        model.addAttribute("page",page);
        model.addAttribute("hasPrevious",page.hasPrevious());
        model.addAttribute("hasNext",page.hasNext());
        return "admin/types";
    }

    /**
     * 跳转到添加类型的页面
     */
    @GetMapping("/types/toInput")
    public String toInput(){
        return "admin/types-input";
    }

    /**
     * 添加类型
     */
    @PostMapping("/types")
    public String insertType(TType type,
                             RedirectAttributes attributes){

        Boolean hasType = typeService.getTypeByName(type);
        if (hasType){
            attributes.addFlashAttribute("message","标签已存在");
            return "redirect:/admin/types/toInput";
        }

        int result = typeService.saveType(type);
        if (result>0){
            //保存成功
            attributes.addFlashAttribute("message","操作成功");
        } else {
            //保存失败
            attributes.addFlashAttribute("message","操作失败");
        }
        return "redirect:/admin/types";
    }

    /**
     * 修改类型
     */
    @PostMapping("/types/{id}")
    public String updateType(TType type,
                             @PathVariable Long id,
                             RedirectAttributes attributes){

        Boolean hasType = typeService.getTypeByName(type);
        if (hasType){
            attributes.addFlashAttribute("message","标签已存在");
            return "redirect:/admin/types/toInput";
        }

        int result = typeService.updateType(type);
        if (result>0){
            //保存成功
            attributes.addFlashAttribute("message","更新成功");
        } else {
            //保存失败
            attributes.addFlashAttribute("message","更新失败");
        }
        return "redirect:/admin/types";
    }

    /**
     * 查询类型并跳转
     */
    @GetMapping("/types/{id}/input")
    public String toUpdate(@PathVariable Long id, Model model){
        TType type = typeService.getTypeById(id);
        model.addAttribute("type",type);
        return "admin/types-input";
    }

    /**
     * 删除类型
     */
    @GetMapping("/types/{id}/delete")
    public String delete(@PathVariable Long id){
        typeService.deleteType(id);
        return "redirect:/admin/types";
    }

}
